package mao.leetcode.others;

public class leetcode134 {
    public static void main(String[] args) {
        leetcode134 leetcode134 = new leetcode134();
        int[] a = {1,2,3,4,5};
        int[] b = {3,4,5,1,2};
        leetcode134.canCompleteCircuit(a,b);
    }

    public int canCompleteCircuit(int[] gas, int[] cost) {
        int n = gas.length;
        int spare = 0;
        int minSpare = Integer.MAX_VALUE;
        int minIndex = 0;
        for(int i=0;i<n;i++){
            spare+=gas[i]-cost[i];
            if(spare < minSpare){
                minIndex = i;
                minSpare = spare;
            }
        }
        return spare >=0?(minSpare+1)%n:-1;
    }
}
